import { ref, computed, reactive, shallowRef } from 'vue'
import { defineStore } from 'pinia'

export const useCounterStore = defineStore('counter', () => {
  const count = ref(0)
  const doubleCount = computed(() => count.value * 2)
  function increment() {
    count.value++
  }

  const Logins = reactive({
    UserId: '',
    Nickname: '',
    Account: '',
    Email: '',
    RoleId: '',
    RoleName: '',
    Roles: [] as any[],
    MenuName: '',
    Url: '',
    SortOrder: '',
    token: ''
  })

  function setRoles(roles: any[]) {
    Logins.Roles = Array.isArray(roles) ? roles : []
    if (Logins.Roles.length > 0) {
      const r = Logins.Roles[0]
      Logins.RoleId = r?.roleid ?? r?.roleId ?? ''
      Logins.RoleName = r?.roleName ?? r?.alias ?? ''
    }
  }

  function setCurrentRoleByIndex(index: number) {
    const r = Logins.Roles[index]
    if (r) {
      Logins.RoleId = r?.roleid ?? r?.roleId ?? ''
      Logins.RoleName = r?.roleName ?? r?.alias ?? ''
    }
  }

  return { count, doubleCount, increment, Logins, setRoles, setCurrentRoleByIndex }
},{persist:true})
